﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;
namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class DepartmentsBLL:IDisposable
    {
        DatabaseContext _DatabaseContext;
        public DepartmentsBLL():this(new DatabaseContext())
        { 
        }

        public DepartmentsBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public IQueryable<Departments> GetAllDepartments()
        {
            return _DatabaseContext.Departments.Where(d => !d.IsDeleted);
        }

        public IQueryable<Departments> GetAllActiveDepartments(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Departments> departments = _DatabaseContext.Departments.Where(d =>!d.IsDeleted);
            totalRowCount = departments.Count();
            return departments.OrderBy(d=>d.Name).Skip(startRowIndex).Take(maximumRows);
        }

        public void AddDepartment(Departments department)
        {
            _DatabaseContext.Departments.Add(department);
            _DatabaseContext.SaveChanges();
        }

        public Departments GetDepartmentByID(int id)
        {
            return _DatabaseContext.Departments.Where(d => d.ID == id).FirstOrDefault();
        }

        public void SaveChanges()
        {
            _DatabaseContext.SaveChanges();
        }
    }
}